<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>epg测试页面</title>

    <link media="all" type="text/css" href="./css/ui.css?v=2014122229711" rel="stylesheet">
    <link media="all" type="text/css" href="./css/pps.css?v=2014122229711" rel="stylesheet">
    <script src="./js/core.js?v=2014122229711" type="text/javascript"></script>
    <script src="./js/ui.js?v=2014122229711" type="text/javascript"></script>
    <script src="./js/qui.js?v=2014122229711" type="text/javascript"></script>


<body>

</body>
<script type="text/javascript">
window.Statistic = {version: 1.0};
Statistic.Dao = $.extend({}, {
    sendProxy: function (options, headers) {
        var requestData = {url: options.url, type: options.type, params: options.data, needCookie: true};
        if (headers != null) {
            requestData.headers = headers;
        }
        var newOptions = {
            url: "/api/redirect",
            type: "post",
            dataType: "json",
            data: {"request": JSON.stringify(requestData), timeSign: new Date().getTime()},
            success: function (data) {
                options.success(data);
            },
            error: function (data) {
                options.error(data);
            }
        }
        $.ajax(newOptions);
    },
    getMaterialList: function (sendData, handler) {
        var url = "http://10.23.85.152:8500/vod/ad/getList";
        var options = {
            type: "post",
            url: url,
            dataType: "json",
            data: {paramsObj: JSON.stringify(sendData), pageSize: 10, pageNo: 1},
            success: function (data) {
                handler.call(this, data);
            },
            error: function (data) {
                handler.call(this, data);
            }
        }
        this.sendProxy(options, {Sign: "#f93Uc31K24()_@"})
    }
})
Statistic.Process = function (options) {
    this.regKey = options.regKey;
    //   this.logArr = [];
    this.logEntity = {
        logArr: [],
        push: function (logData) {
            this.logArr.push(logData);
        }
    }
    this.isStop = false;
    this.statusDescr = null;
    this.timerId = -1;
    this.treeObj = {};
    this.methodObj = {};
    this.rootKey = null;
    this.currentKey = null;
}
Com.inherits(Statistic.Process, {}).extend({
    startProcess: function () {
        console.log("[regKey=" + this.regKey + "]process start");
        var self = this;
        if (self.timerId != -1) {
            clearInterval(self.timerId);
        }
        self.timerId = setInterval(function () {
            if (!self.isStop && !self.isWork) {
                self.callNext(self.currentKey)
            }
        }, 300)
    },
    stopProcess: function () {
        clearInterval(this.timerId);
        this.isStop = true;
        this.isWork = false;
        console.log("[regKey=" + this.regKey + "]process over[status=" + this.statusDescr + "]");
        console.log(JSON.stringify(this.logEntity));
    },
    callNext: function (nodeKey) {
        console.log("callNext: " + nodeKey);
        var node = this.treeObj[nodeKey];

        if (node != null) {
            var method = this.methodObj[nodeKey];
            this.logEntity.push("[start node:" + nodeKey + "][" + Date.now() + "]");
            this.isWork = true;
            if (node.needResult != null) {
                method.call(this, this.treeObj[node.needResult].result);
            } else {
                method.call();
            }
        } else {
            this.statusDescr = "complete";
            this.stopProcess();
        }
    },
    addOrderFunObj: function (nodeKey, options) {
        var self = this;
        options = $.extend(true, {needProxy: false, judge: function (data) {
            return true;
        }}, options);
        var wrapHandler = function (data) {
            self.logEntity.push("[end nodeKey:" + nodeKey + "][" + Date.now() + "]");
            self.treeObj[nodeKey].result = data;
            if (!options.judge(data)) {
                self.statusDescr = "interrupt";
                self.stopProcess();
            } else {
                self.currentKey = self.treeObj[nodeKey].nextKey;
            }
            self.isWork = false;
        }

        var tempOptions = $.extend({}, options, {
            success: function (data) {
                options.success(data);
                wrapHandler(data);
            },
            error: function (data) {
                options.error(data);
                wrapHandler(data);
            }
        })
        self.methodObj[nodeKey] = function () {
            var finalOptions = tempOptions;
            var result = null;
            if (finalOptions.needResult != null) {
                if (finalOptions.needResult in self.treeObj) {
                    result = self.treeObj[finalOptions.needResult].result
                }
                if(typeof finalOptions.checkNeedResult == "function"){
                    if( !finalOptions.checkNeedResult( result )){
                        console.log("checkNeedResult fail" )
                        self.statusDescr = "interrupt";
                        self.stopProcess();
                        return ;
                    }
                }
            }
            if (options.needProxy && typeof  options.beforeQuery == "function") {
                finalOptions = options.beforeQuery.call(this, finalOptions, result);
            }
            $.ajax(finalOptions);
        }
    },
    addProcessTree: function (node) {
        this.treeObj[node.key] = node;
        if (node.root == true) {
            this.rootKey = node.key;
            this.currentKey = node.key;
        }
    }
});

$(document).ready(function () {
    console.log("load complete");
    var demo = new Statistic.Process({regKey: "test"});
    demo.addOrderFunObj("getType1", {
        url: "/api/test/msg",
        success: function (data) {
            console.log(data);
        },
        error: function (data) {
            console.log(data);
        }
    })
    demo.addOrderFunObj("getType2", {
        url: "/api/test/msg",
        success: function (data) {
            console.log(data);
        },
        error: function (data) {
            console.log(data);
        },
        needProxy: true,
        // needResult: "getType1",
        beforeQuery: function (options, resultData) {
            console.log("beforeQuery " + JSON.stringify(resultData));
            return options;
        }
    })
    demo.addOrderFunObj("getType2", {
        url: "/api/test/msg",
        success: function (data) {
            console.log(data);
        },
        error: function (data) {
            console.log(data);
        },
        needProxy: true,
        // needResult: "getType1",
        beforeQuery: function (options, resultData) {
            console.log("beforeQuery " + JSON.stringify(resultData));
            return options;
        }
    })

    demo.addOrderFunObj("redirect3", {
        url: "http://10.23.85.152:8500/vod/ad/getList",
        type:"post",
        success: function (data) {
            console.log(data);
        },
        error: function (data) {
            console.log(data);
        },
        needProxy: true,
        needResult: "getType2",
        checkNeedResult:function(resultData){
            return false;
        },
        data:{paramsObj: JSON.stringify({}) ,pageSize:10,pageNo:1},
        beforeQuery: function (options, resultData) {
            console.log("beforeQuery " + JSON.stringify(resultData));
            var requestData = {url: options.url, type: options.type, params: options.data, needCookie: true};
            requestData.headers = {Sign: "#f93Uc31K24()_@"};
            var newOptions = {
                url: "/api/redirect",
                type: "post",
                dataType: "json",
                data: {"request": JSON.stringify(requestData), timeSign: new Date().getTime()},
                success: function (data) {
                    options.success(data);
                },
                error: function (data) {
                    options.error(data);
                }
            }
            return newOptions;
        }
    })

    demo.addProcessTree({key: "getType1", nextKey: "getType2", root: true});
    demo.addProcessTree({key: "getType2", nextKey: "redirect3" });
    demo.addProcessTree({key: "redirect3" });
    demo.startProcess();
})
</script>
</html>